﻿using System;
using System.Windows.Documents;
using System.Collections.Generic;
using AccessControl.Contracts.Models;
using System.Windows;

namespace AccessControl.Wpf.Printing
{
	class EventsPaginator : DocumentPaginator
	{
		#region Fields

		private Size _pageSize;
		private int _rowsPerPage;
		
		#endregion

		#region Public Constructor

		public EventsPaginator()
		{
		}

		#endregion

		#region Public Properties

		public IList<IEvent> Events
		{
			get;
			set;
		}

		#endregion

		#region DocumentPaginator Overrides

		public override DocumentPage GetPage(int pageNumber)
		{
			int currentRow = _rowsPerPage * pageNumber;

			var page = new EventPageElement()
			{
				CurrentRow = currentRow,
				Rows = Math.Min(_rowsPerPage, Events.Count - currentRow),
				Events = Events,
				Width = PageSize.Width,
				Height = PageSize.Height,
			};

			page.Measure(PageSize);
			page.Arrange(new Rect(new Point(0, 0), PageSize));

			return new DocumentPage(page);
		}

		public override bool IsPageCountValid
		{
			get { return true; }
		}

		public override int PageCount
		{
			get { return (int)Math.Ceiling(Events.Count / (double)_rowsPerPage); }
		}

		public override Size PageSize
		{
			get { return _pageSize; }
			set
			{
				_pageSize = value;
				_rowsPerPage = EventPageElement.RowsPerPage(PageSize.Height);
			}
		}

		public override IDocumentPaginatorSource Source
		{
			get { return null; }
		}

		#endregion

	}
}
